package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.DeptService;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {


    @Autowired
    private DeptService deptService;

    @Autowired
    private EmpExprService empExprService;
    @Autowired
    private EmpMapper empMapper;

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {

        Page<Emp> p = Page.of(param.getPage(),param.getPageSize());

        p.addOrder(OrderItem.desc("entry_date"));
        LambdaQueryWrapper<Emp> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(param.getName()!=null && !param.getName().isEmpty(),Emp::getName,param.getName())
                .eq(param.getGender()!=null && !param.getName().isEmpty(),Emp::getGender,param.getGender())
                .between(param.getBegin()!=null && param.getEnd()!= null,Emp::getEntryDate,param.getBegin(),param.getEnd());








        Page<Emp> page = page(p,wrapper);

        List<Emp> list = page.getRecords();

        Set<Integer> deptIds = list.stream().map(emp -> emp.getDeptId()).collect(Collectors.toSet());
        List<Dept> deptList = new ArrayList<>();
        if (!deptIds.isEmpty()) {
            deptList = deptService.listByIds(deptIds);
        }
        Map<Integer, String> deptMap = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));


        for (Emp emp : list) {
            Integer deptId = emp.getDeptId();
            emp.setDeptName(deptMap.get(deptId));

        }









        return new PageResult<>(page.getTotal(),list);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        save(emp);

        List<EmpExpr> exprList = emp.getExprList();
        for (EmpExpr empExpr : exprList) {
            empExpr.setEmpId(emp.getId());
        }

        empExprService.saveBatch(exprList);



    }

    @Override
    public Emp getEmpById(Integer id) {

        Emp emp = empMapper.selectById(id);

        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,id);

        List<EmpExpr> list = empExprService.list(wrapper);
        emp.setExprList(list);

        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);

        LambdaQueryWrapper<EmpExpr> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EmpExpr::getEmpId,emp.getId());
        empExprService.remove(wrapper);
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(expr -> expr.setEmpId(emp.getId()));
        }
        empExprService.saveBatch(exprList);



    }
}
